package com.zhulanhua.blogs.controller;


import com.zhulanhua.blogs.Utils.Code;
import com.zhulanhua.blogs.Utils.DozerUtils;
import com.zhulanhua.blogs.Utils.Result;
import com.zhulanhua.blogs.Utils.StringUtils;
import com.zhulanhua.blogs.controller.VO.ArticleDetailVO;
import com.zhulanhua.blogs.controller.VO.ArticleVO;
import com.zhulanhua.blogs.controller.params.ArticleParams;
import com.zhulanhua.blogs.domain.Article;
import com.zhulanhua.blogs.domain.query.ArticleQuery;
import com.zhulanhua.blogs.service.ArticleService;
import com.zhulanhua.blogs.service.CommentService;
import com.zhulanhua.blogs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章接口
 * */
@RestController
@RequestMapping("/api/articles")
@CrossOrigin
public class ArticleController {

    @Autowired
    ArticleService articleService;
    @Autowired
    DozerUtils dozerUtils;
    @Autowired
    CommentService commentService;
    @Autowired
    UserService userService;

    /**
     * 获取最新文章列表
     * @param page  第几页，默认为1
     * @param limit 一页多少个，默认为10
     * */
    @GetMapping("/all")
    public Result<List<ArticleVO>> getAll(@RequestParam(value = "page",defaultValue = "1") int page,
                                        @RequestParam(value = "limit",defaultValue = "10") int limit){
        ArticleQuery articleQuery = new ArticleQuery();
        articleQuery.setSort("desc,create_time");
        articleQuery.setPage(page);
        articleQuery.setPageNum(limit);
        return new Result<>(Code.OK, articleService.getQuery(articleQuery));
    }

    /**
     * 自定义筛选文章列表
     * @param articleQuery
     * */
    @GetMapping("/query")
    public Result<List<ArticleVO>> getQuery(@Validated ArticleQuery articleQuery){
        return new Result<>(Code.OK, articleService.getQuery(articleQuery));
    }

    /**
     * 获取热门文章列表
     * @param page  第几页，默认为1
     * @param limit 一页多少个，默认为10
     * */
    @GetMapping("/hot")
    public Result<List<ArticleVO>> getQuery(@RequestParam(value = "page",defaultValue = "1") int page,
                                            @RequestParam(value = "limit",defaultValue = "10") int limit){
        ArticleQuery articleQuery = new ArticleQuery();
        articleQuery.setPage(page);
        articleQuery.setPageNum(limit);
        articleQuery.setSort("desc,browse,support,create_time");
        return new Result<>(Code.OK, articleService.getQuery(articleQuery));
    }

    /**
     * 获取文章详情
     * @param id 文章id
     * */
    @GetMapping("/{id}")
    public Result<ArticleDetailVO> getDateils(@PathVariable long id){
        ArticleDetailVO dateils = articleService.getDateils(id);

        return new Result<>(Code.OK, dateils);
    }

    /**
     * 上传文章
     * @param article
     * */
    @PostMapping("/add")
    public Result save(@Validated @RequestBody ArticleParams article){
        Article convert = dozerUtils.convert(article, Article.class);
        articleService.save(convert);
        return Result.ok();
    }

    /**
     * 删除文章
     * @param id 文章id
     * */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable long id){
        articleService.delete(id);
        return Result.ok();
    }

    /**
     * 文章点赞
     * @param id 文章id
     * */
    @PostMapping("/praise")
    public Result praiseSave(@RequestBody long id){
        articleService.praise(id, true);
        return Result.ok();
    }

    /**
     * 文章取消点赞
     * @param id 文章id
     * */
    @DeleteMapping("/praise")
    public Result praiseDelete(@RequestBody long id){
        articleService.praise(id, false);
        return Result.ok();
    }
}
